<script setup>
import { onMounted, ref } from 'vue'
import { getRecentSongApi } from '@/apis/song.js'
import { useAudioStore } from '@/stores/audioStore';
import ListCard from '@/components/ListCard/ListCard.vue'

const useAudio = useAudioStore()

const recentSong = ref([])
// 获取最近播放的歌曲
onMounted(async () => {
  const { data } = await getRecentSongApi()
  recentSong.value = data.data.list
})

// 将当前列表保存到播放列表
const playRecentSong = () => {
  useAudio.activeSongList = []
  recentSong.value.forEach(item => {
    useAudio.activeSongList.push(item.data)
  })
}

</script>

<template>
  <div class="play-history" v-if="recentSong.length">
    <li class="song-list-tips">
      <div class="song-info">
        <span class="index">序号</span>
        <span class="song-name">歌曲名称</span>
        <span class="song-author">作者</span>
      </div>
      <span class="play-duration ellipsis">歌曲时长</span>
      <span class="play-time ellipsis">播放时间</span>
      <span class="set ellipsis">操作</span>
    </li>
    
    <ListCard v-for="(item, index) in recentSong" :key="item.id" :song="item.data" :index="index" @play="playRecentSong">
      <span v-unix-format="item.playTime" class="ellipsis"></span>
    </ListCard>
  </div>

  <!-- 空状态 -->
  <div class="none-data" v-else>
    <img src="@/assets/noneData.png" alt="">
    <p>空空如也</p>
  </div>
  <!-- / 空状态 -->
</template>

<style scoped lang='scss'>
.play-history {
  max-width: 1165px;
  margin: 0 auto;

  .song-list-tips {
    margin: 10px 0px;
    color: $assistText;
    display: flex;
    align-items: center;
    padding: 10px;
    background-color: rgba(255, 255, 255, 0.4);
    border-radius: 8px;

    .song-info {
      display: flex;
      align-items: center;
    }

    span {
      display: block;
    }

    .index {
      width: 30px;
      margin-left: 23px;
    }

    .song-name {
      margin-left: 10px;
      width: 260px;
      text-align: center;
    }

    .song-author {
      margin-left: 60px;
      width: 220px;
      text-align: center;
    }

    .play-duration{
      width: 140px; 
      text-align: center;
      margin-left: 67px;
    }
    .play-time {
      width: 230px;
      text-align: center;
    }
    .set {
      width: 50px;
      text-align: center;
      margin-left: 38px;
    }
  }
}

.none-data {
  width: 500px;
  margin: 70px auto;

  img {
    width: 100%;
    height: 100%;
  }

  p {
    text-align: center;
    color: #949494;
    font-weight: bold;
    margin-bottom: 50px;
    letter-spacing: 2px;
    font-size: 15px;
  }
}
</style>
